import 'package:demo_app_video/store/user.dart';
import 'package:dio/dio.dart';
import 'package:uuid/uuid.dart';

class HttpUtil {
  static HttpUtil _instance = HttpUtil();
  final isProd = bool.fromEnvironment('dart.vm.product');

  static Dio dio = Dio(BaseOptions(baseUrl: 'https://hpg.rongqide.cn/api/'));

  static HttpUtil getInstance() {
    return _instance;
  }

  //单例
  HttpUtil() {
    dio.interceptors.add(InterceptorsWrapper(
        //dio---请求拦截器
        onRequest: (RequestOptions options, RequestInterceptorHandler handler) {
      print("====>lanjieqi");
      print(options.queryParameters);
      print("====>lanjieqi");
      print("====>lanjieqi");
      if ($UserStore.token == '') {
        options.headers['useruuid'] = $UserStore.uuid;
      } else {
        options.headers['token'] = $UserStore.token;
      }
      return handler.next(options);
    }));

    // dio.interceptors.add(LogInterceptor(responseBody: true));
  }
  // get 请求封装
  Future get(url, {options, cancelToken, data}) async {
    // print(data);

    Response? response;
    try {
      response =
          await dio.get(url, queryParameters: data, cancelToken: cancelToken);
    } on DioError catch (e) {
      response = e!.response;
    }

    return response!.data;
  }

  // post请求封装
  post(url, {options, cancelToken, data = null, is_tally = 1}) async {
    Response? response;
    dio.options.contentType = Headers.formUrlEncodedContentType;
    try {
      response = await dio.post(url,
          data: data != null ? data : {}, cancelToken: cancelToken);
    } on DioError catch (e) {
      response = e.response;
      // Toast.getInstance().showError(
      //   msg: '后台系统错误'
      // );
      // throw new Exception(e.response.statusCode);
    }
    return response!.data;
  }

  // delete请求封装
  delete(url, {options, cancelToken, data = null}) async {
    Response? response;
    try {
      response = await dio.delete(url,
          data: data != null ? data : {}, cancelToken: cancelToken);
    } on DioException catch (e) {
      response = e.response;
    }
    return response!.data;
  }
}
